import JSZip from 'jszip';
import {saveAs} from 'file-saver';
import http from './utils/request'

// 链接为临时链接 请转为自己下载图片链接
const data = [
    {
        fileUrl: 'https://yangbao.oss-cn-shanghai.aliyuncs.com/wx/bg/top-bg.png',
        fileName: 'top-bg.png'
    },
    {
        fileUrl: 'https://yangbao.oss-cn-shanghai.aliyuncs.com/wx/bg/DPlayer.png',
        fileName: 'DPlayer.png'
    }
];

const getFile = (url) => {
    return new Promise((resolve, reject) => {
        http({
          method: 'get',
          url,
          responseType: 'arraybuffer'
        }).then((res) => {
            resolve(res)
        }).catch((error) => {
            reject(error)
        })
    })
}

document.getElementById('downLoad').onclick = function () {
    const zip = new JSZip();
    const promiseArr = []
    data.forEach((item) => {
        const promise = getFile(item.fileUrl).then((res) => {
          zip.file(item.fileName, res ,{binary: true});
        })
        promiseArr.push(promise)
    })
    Promise.all(promiseArr).then(()=>{
        console.log(promiseArr,zip)
        zip.generateAsync({
            type: "blob",
            compression: "DEFLATE",  // STORE：默认不压缩 DEFLATE：需要压缩
            compressionOptions: {
                level: 9               // 压缩等级1~9    1压缩速度最快，9最优压缩方式
            }
        }).then(res=>{
            saveAs(res, "压缩包.zip")
        })
    })
}
